/**
 * PersonalUserRecord State
 *
 * @date 2020-08-24
 * @author 逗号米
 */

import Vue from 'vue'
import { GetterTree, MutationTree, ActionTree, Commit, Module } from 'vuex'

import * as http from '@/http/cms/personal/personal-login-record'
import { arrayFindIndex, isArray } from '@/utils'

// 接口
export interface State {
  currentPage: number,
  perPage: number,
  total: number,
  loginRecords: Array<any>,
  loginRecord: any,
  userLoginRecords: any,
}

// 状态
const state: State = {
  currentPage: 1,
  perPage: 20,
  total: 0,
  loginRecords: [],
  loginRecord: {},
  userLoginRecords: {}
}

const getters: GetterTree<State, any> = {}

const mutations: MutationTree<State> = {
  /**
   * 分页
   *
   * @param state
   * @param current_page
   * @param page_size
   * @param total
   */
  page(state: State, {
    data_list,
    current_page,
    per_page,
    total
  }: { data_list: any, current_page: number, per_page: number, total: number }): void {
    state.loginRecords = data_list
    state.currentPage = current_page
    state.perPage = per_page
    state.total = total
  },
  
  /**
   * 显示
   *
   * @param state
   * @param data
   */
  show(state: State, data: any) {
    state.loginRecord = data
  },

  /**
   * 删除
   *
   * @param state
   * @param ids
   */
  destroy(state: State, ids: Array<number>) {
    if (isArray(ids)) {
      ids.forEach((id) => {

        // 删除主列表
        if (state.loginRecords.length > 0) {
          const index = arrayFindIndex(state.loginRecords, 'id', id)
          index >= 0 && state.loginRecords.splice(index, 1)
        }

        // 删除用户列表
        if (state.userLoginRecords.data_list.length > 0) {
          const index = arrayFindIndex(state.userLoginRecords.data_list, 'id', id)
          index >= 0 && state.userLoginRecords.data_list.splice(index, 1)
        }
      })
    }
  },

  /**
   * 按简历查询投递记录
   *
   * @param state
   * @param data
   */
  userLoginRecords(state: State, data: any) {
    state.userLoginRecords = data
  }
}

const actions: ActionTree<State, any> = {
  /**
   * 首页
   *
   * @param commit
   * @param {Object} payload 负载
   * @return {Promise<any>}
   */
  index({commit}: { commit: Commit }, payload: any): Promise<any> {
    return  http.index(payload.params).then((res) => {
      if (res.status === 200) {
        commit('page', res.data)
      }
      return res
    })
  },
  
  /**
   * 查询
   *
   * @param commit
   * @param {Object} [payload] 负载
   * @return {Promise<any>}
   */
  query({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.query(payload.params).then((res) => {
      if (res.status === 200) {
        commit('page', res.data)
      }
      return res
    })
  },
  
  /**
   * 显示
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  show({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.show(payload.id).then((res) => {
      if (res.status === 200) {
        commit('show', res.data)
      }
      return res
    })
  },

  /**
   * 按用户查询用户登录
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  userLoginRecord({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.user(payload.uid, payload.params).then((res) => {
      if (res.status === 200) {
        commit('userLoginRecords', res.data)
      }
      return res
    })
  }
}

export const personalUserRecord: Module<State, any> = {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}

export default {
  personalUserRecord
}
